"use strict";
var app = angular.module("myapp", []);

app.controller("TaskController", function($scope) {
	var data = eval("(" + tasks + ")");
	console.log("长度：" + data.length);
	var undone_tasks = [];
	var done_tasks = [];
	var expired_tasks = [];
	for ( var i = 0, n = data.length; i < n; i++) {
		console.log(data[i]);
		var task = data[i];
		if (task.isExpired == 1) {
			expired_tasks.splice(0, 0, task);
		} else if (task.hasDone == 1) {
			done_tasks.splice(0, 0, task);
		} else {
			undone_tasks.splice(0, 0, task);
		}
	}
	console.log(data);
	$scope.expired_tasks = expired_tasks;
	$scope.undone_tasks = undone_tasks;
	$scope.done_tasks = done_tasks;
	$scope.mousein = function(row) {
		$scope.selectedRow = row;
	};
	$scope.download = function(row,index) {
		var task = null;
		switch (index) {
		case 1:
			task = $scope.undone_tasks[row];
			break;
		case 2:
			task = $scope.done_tasks[row];
			break;
		case 3:
			task = $scope.expired_tasks[row];
			break;
		default:
			break;
		}
		location.href = "download?fileID=" + task.attachID;
	};
	$scope.remove = function(row) {
		// delete/courseware?id={{row.coursewareID}}
		var url = "delete/task?id=" + $scope.tasks[row].id;
		console.log("请求地址：" + url);
		location.href = url;
	};
	$scope.submit = function(row, index) {
		var taskid = -1;
		switch (index) {
		case 1:
			taskid = $scope.undone_tasks[row].id;
			$('#homeworkid').attr("value", -1);
			break;
		case 2:
			taskid = $scope.done_tasks[row].id;
			$('#homeworkid').attr("value", $scope.done_tasks[row].homework.id);
			break;
		default:
			break;
		}

		$('#taskid').attr("value", taskid);
		$('#modal_homework').modal();
	};
	$scope.preview = function(row, index) {
		var task = null;
		switch (index) {
		case 2:
			task = $scope.done_tasks[row];
			break;
		case 3:
			task = $scope.expired_tasks[row];
			break;
		default:
			break;
		}
		var homework = task.homework;
		if (homework == null) {
			alert("没有提交过该作业");
			return;
		}
		$('#preview_homework_content').html(homework.content);
		if (homework.attachID == -1) {
			$('#preview_homework_attach').html("没有附件");
		} else {
			$('#preview_homework_attach').html("<a href=\"download?fileID=\"" + homework.attachID+
					" data-toggle=\"tooltip\" data-placement=\"left\" title=\"下载\">" +
					"<span class=\"glyphicon glyphicon-link\">&nbsp;" +homework.attach+
					"</span></a>");
		}
		$('#preview_homework_note').html(homework.note);
		$('#preview_homework_pass').html(homework.pass);
		$('#preview_homework_score').html(homework.score);
		$('#preview_homework_evaluation').html(homework.evaluation);
		$('#preview_modal_homework').modal();
	};
});

// 文件上传
$(function() {
	// $('input[type=file]').bootstrapFileInput();
	
	var submit_btn = $('#submit_btn');
	var reset_btn = $('#reset_btn');
	

	var bar = $('.progress-bar');
	var status = $('#status');
	var percentValue = $('#percentValue');
	
	$('#new_homework_form').ajaxForm({
		beforeSend : function() {
			status.empty();
			percentValue.empty();
			var percentVal = '0%';
			percentValue.html(percentVal);
			bar.width(percentVal);
			submit_btn.attr("disabled", "disable");
			reset_btn.attr("disabled", "disable");
		},
		uploadProgress : function(event, position, total, percentComplete) {
			var percentVal = percentComplete + '%';
			percentValue.html(percentVal);
			bar.width(percentVal);
		},
		success : function() {
			var percentVal = '100%';
			percentValue.html(percentVal);
			bar.width(percentVal);
			status.html("上传完成，正在处理... 请稍等");
		},
		complete : function(xhr) {
			console.log(xhr.responseText);
			var res = eval("(" + eval(xhr.responseText) + ")");
			console.log(res);
			submit_btn.removeAttr("disabled");
			submit_btn.removeClass("disabled");
			reset_btn.removeAttr("disabled");
			reset_btn.removeClass("disabled");
			if (res.error != 0) {
				// alert(res.msg);
				$("#submit-btn").removeAttr("disabled");
				$("#submit-btn").removeClass("disabled");
				percentValue.html('0%');
				bar.width('0%');
				status.html(res.msg);
			} else {
				percentValue.html('0%');
				bar.width('0%');
				status.empty();
				status.html("提交成功,3秒后自动关闭。。。");
				setTimeout(function() {
					$('#reset_btn').click();
					$('#modal_homework').modal('hide');
				}, 3000);
			}

			// status.html("");

		}
	});

});